export interface Serializer {
  serialize(obj: any): string;
  deserialize(data: string): any;
}

// 默认的 JSON 序列化器实现
export class JsonSerializer implements Serializer {
  serialize(obj: any): string {
    try {
      console.log(`[SERIALIZER] Serializing object of type: ${obj.type || typeof obj}`);
      const result = JSON.stringify(obj);
      console.log(`[SERIALIZER] Serialization successful, result length: ${result.length}`);
      return result;
    } catch (error: any) {
      console.error(`[SERIALIZER] Serialization failed: ${error.message}`);
      console.error(`[SERIALIZER] Problematic object keys:`, Object.keys(obj));
      throw error;
    }
  }

  deserialize(data: string): any {
    try {
      console.log(`[SERIALIZER] Deserializing string of length: ${data.length}`);
      const result = JSON.parse(data);
      console.log(`[SERIALIZER] Deserialization successful, result type: ${result.type || typeof result}`);
      return result;
    } catch (error: any) {
      console.error(`[SERIALIZER] Deserialization failed: ${error.message}`);
      console.error(`[SERIALIZER] Data preview:`, data.substring(0, 100) + (data.length > 100 ? '...' : ''));
      throw error;
    }
  }
} 